package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController("adminOrderController")
@Slf4j
@RequestMapping("/admin/order")
@Api(tags = "订单管理接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索
     * @param beginTime
     * @param endTime
     * @param number
     * @param phone
     * @param status
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> search(LocalDateTime beginTime, LocalDateTime endTime, String number,String phone, Integer status, int page, int pageSize) {
        PageResult pageResult = orderService.search(beginTime,endTime,number,phone,status,page,pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/statistics")
    @ApiOperation("各状态订单数量统计")
    public Result<OrderStatisticsVO> countOrderByStatus() {
        log.info("各状态订单数量统计");
        OrderStatisticsVO orderStatisticsVO =orderService.countOrderByStatus();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result queryOrderDetails(@PathVariable Long id) {
        log.info("查询订单详情");
        return Result.success(orderService.getOrderDetails(id));
    }

    /**
     * 接单
     *
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 商家拒单
     * @param ordersRejectionDTO 拒单实体类
     * @return
     */
    @ApiOperation("商家拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("商家拒单：{}",ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 商家取消订单
     * @param cancelDTO 取消订单实体类
     * @return
     */
    public Result cancleOrder(@RequestBody OrdersCancelDTO cancelDTO) {
        log.info("商家取消订单：{}",cancelDTO);
        orderService.adminCancle(cancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id 订单id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("订单派送")
    public Result delivery(@PathVariable Long id) {
        log.info("订单派送：{}",id);
        orderService.delivery(id);
        return Result.success();
    }
    @PutMapping("/complete/{id}")
    @ApiOperation("订单完成")
    public Result complete(@PathVariable Long id) {
        log.info("订单完成：{}",id);
        orderService.complete(id);
        return Result.success();
    }

}
